package com.yootk.action;

import com.yootk.action.abs.AbstractAction;
import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.HashMap;
import java.util.Map;

@Controller

@RequestMapping("/pages/web/*")
public class InnerObjectAction extends AbstractAction {
    @GetMapping("object")
    @ResponseBody
    public Object object(){
        Map<String,Object> map = new HashMap<>() ;
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        HttpServletResponse response = requestAttributes.getResponse();
        HttpSession session = request.getSession();
        ServletContext servletContext = request.getServletContext();
        map.put("getContextPath",request.getContextPath()) ;
        map.put("getId",session.getId()) ;
        map.put("getCharacterEncoding",servletContext.getResponseCharacterEncoding()) ;
        map.put("getLocale",response.getLocale()) ;
        return map ;

    }
    @GetMapping("action")
    @ResponseBody
    public Object action(HttpServletRequest request,HttpServletResponse response,HttpSession session){
        Map<String,Object> map = new HashMap<>() ;

        ServletContext servletContext = request.getServletContext();
        map.put("getContextPath",servletContext.getContextPath()) ;
        map.put("getId",session.getId()) ;
        map.put("getCharacterEncoding",response.getCharacterEncoding()) ;
        map.put("getLocale",response.getLocale()) ;
        return map ;

    }

}
